package com.example.zuoye;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.ConditionVariable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextClock;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText etName,etPhone;
    private Button btnAdd,btnUpdate,btndelete,btnQuery;
    private TextView tvShow;
    private String name,phone;
    private MyHelper myHelper;
    private SQLiteDatabase db;
    private ContentValues values;
    private static final String TABLE_NAME = "info";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        initView();
    }
    public void initView(){
        myHelper = new MyHelper(MainActivity.this);

        etName = findViewById(R.id.et_name);
        etPhone = findViewById(R.id.et_phone);
        tvShow = findViewById(R.id.tv_show);

        btnAdd = findViewById(R.id.btn_add);
        btnUpdate = findViewById(R.id.btn_update);
        btndelete = findViewById(R.id.btn_delete);
        btnQuery = findViewById(R.id.btn_query);

        btnAdd.setOnClickListener(this);
        btnUpdate.setOnClickListener(this);
        btndelete.setOnClickListener(this);
        btnQuery.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if(v.getId()==R.id.btn_add){
            add();
        }else if(v.getId()==R.id.btn_update){
            update();
        }else if(v.getId()==R.id.btn_delete){
            delete();
        }else if(v.getId()==R.id.btn_query){
            query();
        }
    }
    private void showToast(String msg){
        Toast.makeText(this,msg, Toast.LENGTH_SHORT).show();
    }

    //插入数据
    private void add(){
        name = etName.getText().toString().trim();
        phone = etPhone.getText().toString().trim();
        db = myHelper.getWritableDatabase();
        values = new ContentValues();
        values.put("uname",name);
        values.put("phone",phone);
        db.insert(TABLE_NAME,null,values);
        showToast("数据插入成功！");
        db.close();
    }

    //修改数据

    private void update(){
        name = etName.getText().toString().trim();
        phone = etPhone.getText().toString().trim();
        db = myHelper.getWritableDatabase();
        values = new ContentValues();
        values.put("phone",phone);
        db.update(TABLE_NAME,values,"uname=?",new String[]{name});
        //db.update(TABLE_NAME,values,"id=?",new String[]{String.vaule0f(2)});
        showToast("数据修改成功！");
        db.close();
    }

    private void delete(){
        name = etName.getText().toString().trim();
        db = myHelper.getWritableDatabase();
//        db.delete(TABLE_NAME,null,null);//无输入全删
        db.delete(TABLE_NAME,"uname=?",new String[]{name});
        showToast("数据删除成功！");
        db.close();
    }

    private void query(){
        db = myHelper.getReadableDatabase();
        Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, null);
        if(cursor.getCount()==0){
            showToast("数据表为空！");
            String str = "";
            tvShow.setText(str);
        }else{
            String str = "";
            while (cursor.moveToNext()){
                String str1 = cursor.getString(0);
                String str2 = cursor.getString(1);
                String str3 = cursor.getString(2);
                str = str + str1 + "--" + str2 + "--" + str3 + "\n";
            }
            tvShow.setText(str);

        }
    }

}